from datetime import datetime
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel, EmailStr, Field, validator

# -------------------------
# 枚举类型定义（与 SQLAlchemy 的 Enum 对应）
# -------------------------

class UserType(str, Enum):
    JOB_SEEKR = "job_seeker"
    RECRUITER = "recruiter"
    ADMIN = "admin"

class CurrentStatus(str, Enum):
    IN_JOB = "在职-考虑机会"
    UNEMPLOYED = "离职-随时到岗"
    STUDENT = "在校学生"

class ApplicationStatus(str, Enum):
    SUBMITTED = "已投递"
    VIEWED = "已查看"
    INTERVIEWING = "面试中"
    ACCEPTED = "已录用"
    REJECTED = "已拒绝"

class ResourceCategory(str, Enum):
    RESUME_GUIDE = "简历指导"
    INTERVIEW_TIPS = "面试技巧"
    CAREER_PLANNING = "职业规划"
    REGULATIONS = "政策法规"

# -------------------------
# 基础 Pydantic 模型
# -------------------------
class Login(BaseModel):
    username: str
    password: str
class UserBase(BaseModel):
    username: str
    email: EmailStr
    user_type: UserType

class UserCreate(UserBase):
    password: str
    phone: Optional[str] = Field(None)


class UserResponse(UserBase):
    user_id: int
    avatar_url: Optional[str]
    created_at: datetime
    last_login: Optional[datetime]
    is_active: bool

    class Config:
         from_attributes = True  # 允许从 ORM 对象自动转换

# -------------------------
# 其他模型
# -------------------------

class JobPostingBase(BaseModel):
    job_title: str = Field(..., max_length=100)
    company_name: str = Field(..., max_length=100)
    location: str = Field(..., max_length=100)
    salary_range: Optional[str]
    job_description: str
    requirements: str

class JobPostingCreate(JobPostingBase):
    poster_id: int  # 添加poster_id字段
    expiration_date: Optional[datetime] = None  # 添加缺失的expiration_date字段

class JobPostingUpdate(BaseModel):
    job_title: Optional[str] = None
    company_name: Optional[str] = None
    location: Optional[str] = None
    salary_range: Optional[str] = None
    job_description: Optional[str] = None
    requirements: Optional[str] = None
    expiration_date: Optional[datetime] = None
    is_open: Optional[bool] = None
    poster_id: int  # 用于权限验证

class JobPostingResponse(JobPostingBase):
    job_id: int
    post_date: datetime
    expiration_date: Optional[datetime]
    is_open: bool
    user_id: int  # 修改这里，与数据库模型匹配

    class Config:
         from_attributes = True

class JobSeekerProfileBase(BaseModel):
    desired_position: str = Field(..., max_length=100)
    current_status: CurrentStatus
    experience_years: Optional[int] = Field(ge=0)
    expected_salary: Optional[str]
    education_level: Optional[str]
    skills: Optional[str]
    user_id: int  # 添加user_id字段用于关联用户

class JobSeekerProfileCreate(JobSeekerProfileBase):
    pass

class JobSeekerProfileResponse(JobSeekerProfileBase):
    profile_id: int
    user_id: int
    last_updated: datetime

    class Config:
         from_attributes = True

class ApplicationBase(BaseModel):
    cover_letter: Optional[str] = None  # 添加默认值，变为可选字段

class ApplicationCreate(ApplicationBase):
    job_id: int

class ApplicationResponse(ApplicationBase):
    application_id: int
    job_id: int
    applicant_id: int
    apply_time: datetime
    status: ApplicationStatus

    class Config:
         from_attributes = True

class ApplicationUpdate(BaseModel):
    status: ApplicationStatus

class MessageBase(BaseModel):
    content: str
    related_job_id: Optional[int]

class MessageCreate(MessageBase):
    receiver_id: int

class MessageResponse(MessageBase):
    message_id: int
    sender_id: int
    receiver_id: int
    send_time: datetime
    is_read: bool

    class Config:
         from_attributes = True

class ResourceBase(BaseModel):
    title: str = Field(..., max_length=200)
    content: str
    category: ResourceCategory

class ResourceCreate(ResourceBase):
    uploader_id: int

class ResourceResponse(ResourceBase):
    resource_id: int
    uploader_id: int
    uploader_username: str
    upload_time: datetime
    view_count: int

    class Config:
         from_attributes = True

class ForumPostBase(BaseModel):
    title: Optional[str] = Field(None, max_length=200)
    content: str
    parent_post_id: Optional[int] = None

class ForumPostCreate(ForumPostBase):
    user_id: int  # 添加用户ID字段用于关联发布者

class ForumPostResponse(ForumPostBase):
    post_id: int
    user_id: int
    post_time: datetime
    upvotes: int
    downvotes: int

    class Config:
         from_attributes = True

# 添加个人资料更新和密码修改的模型
class UpdateProfile(BaseModel):
    user_id: int
    email: EmailStr
    phone: Optional[str] = None
    current_status: Optional[str] = None
    desired_position: Optional[str] = None
    company_name: Optional[str] = None

class ChangePassword(BaseModel):
    user_id: int
    current_password: str
    new_password: str

class UserUpdate(BaseModel):
    username: Optional[str] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    user_type: Optional[UserType] = None
    is_active: Optional[bool] = None